﻿using LineFocus.Nikcron.Common;
using LineFocus.Nikcron.Models;
using Nickron.Database;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace LineFocus.Nikcron.Controllers
{
    public class StockController : BaseController
    {
        //
        // GET: /Stock/

        public ActionResult Index()
        {
            return View();
        }

        [HttpGet]
        public JsonResult GetStocks(string Status)
        {
            ApplicationDBContext db = new ApplicationDBContext();
            JsonResult result = new JsonResult();
            if(!string.IsNullOrEmpty(Status))
            {
                var Stocks = from p in db.ProductItems.Where(s=>s.InhouseStatus == Status).ToList()
                             select new
                             {
                                 StockId = p.Id,
                                 Brand = p.ProductModel.Manufacture.Name,
                                 ProductType = p.ProductModel.ProductType.Name,
                                 Model = p.ProductModel.ModelNumber,
                                 Stockhouse = p.Stockhouse.Name,
                                 IMEI1 = p.IMEI1,
                                 IMEI2 = p.IMEI2,
                                 SerialNo = p.SerialNumber,
                                 Status = p.InhouseStatus
                             };

                result.Data = Stocks;
                
            }
            else
            {
                var Stocks = from p in db.ProductItems.ToList()
                             select new
                             {
                                 StockId = p.Id,
                                 Brand = p.ProductModel.Manufacture.Name,
                                 ProductType = p.ProductModel.ProductType.Name,
                                 Model = p.ProductModel.ModelNumber,
                                 Stockhouse = p.Stockhouse.Name,
                                 IMEI1 = p.IMEI1,
                                 IMEI2 = p.IMEI2,
                                 SerialNo = p.SerialNumber,
                                 Status = p.InhouseStatus
                             };
                result.Data = Stocks;
            }
            result.JsonRequestBehavior = JsonRequestBehavior.AllowGet;
            return result;
        }

        [HttpGet]
        public ActionResult Maintenance()
        {
            Lookup Lookup_Brands = new Lookup();
            Lookup Lookup_Models = new Lookup();
            Lookup Lookup_ProductTypes = new Lookup();
            Lookup Lookup_Stockhouses = new Lookup();

            ApplicationDBContext db = new ApplicationDBContext();

            var query1 = from records in db.Manufactures
                         select new LookupItem() { Id = records.Id, Name = records.Name };
            Lookup_Brands.Items = query1.ToList();

            var query2 = from records in db.ProductTypes
                         select new LookupItem() { Id = records.Id, Name = records.Name };
            Lookup_ProductTypes.Items = query2.ToList();

            var query3 = from records in db.ProductModels
                         select new LookupItem() { Id = records.Id, Name = records.ModelNumber, ParentId = records.ManufactureId  };
            Lookup_Models.Parent = "Manufacture";
            Lookup_Models.Items = query3.ToList();

            var query4 = from records in db.Offices.OfType<Stockhouse>().ToList()
                         select new LookupItem() { Id = records.Id, Name = records.Name };
            Lookup_Stockhouses.Items = query4.ToList();

            ViewBag.Brands = Lookup_Brands;
            ViewBag.Models = Lookup_Models;
            ViewBag.ProductTypes = Lookup_ProductTypes;
            ViewBag.Stockhouses = Lookup_Stockhouses;
            return View();
        }

        [HttpPost]
        public ActionResult AddStock(FormCollection formCollection)
        {
            ApplicationDBContext db = new ApplicationDBContext();
            ProductItem StockItem = new ProductItem();
            StockItem.StockhouseId = int.Parse(formCollection["Stockhouse"]);
            StockItem.StockInhouse = DateTime.Parse(formCollection["StockDate"]);
            StockItem.Details = "";
            StockItem.UnitPrice.Currency = Currencies.INR.ToString();
            StockItem.UnitPrice.Amount = Double.Parse(formCollection["UnitPrice"]);
            StockItem.IMEI1 = formCollection["IMEIno1"];
            StockItem.IMEI2 = formCollection["IMEIno2"];
            StockItem.SerialNumber = formCollection["SerialNo"];
            StockItem.ProductModelId = int.Parse(formCollection["Model"]);
            StockItem.InhouseStatus = StockStatus.InHouse.ToString();
            db.ProductItems.AddObject(StockItem);
            db.SaveChanges();
            return RedirectToAction("Maintenance");
        }

    }
}
